package com.doublefish.doublefishrobot.ui.video;

import android.app.Dialog;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.doublefish.doublefishrobot.R;
import com.doublefish.doublefishrobot.base.BaseActivity;
import com.doublefish.doublefishrobot.ui.video.adapter.ClearVideoAdapter;
import com.doublefish.doublefishrobot.ui.video.bean.VideoInfoBean;
import com.doublefish.doublefishrobot.ui.video.utils.VideoManager;
import com.doublefish.doublefishrobot.widget.CommonDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 作者:dugaofeng on 2024/10/31 09:43
 */
public class VideoClearActivity extends BaseActivity {


    @BindView(R.id.tv_title)
    TextView titleTv;

    @BindView(R.id.tv_size)
    TextView allSizeTv;
    @BindView(R.id.iv_select)
    ImageView selectIv;

    @BindView(R.id.recycler)
    RecyclerView recyclerView;

    @BindView(R.id.tv_delete)
    TextView deleteTv;

    private ClearVideoAdapter clearVideoAdapter;
    private List<ClearVideoAdapter.ClearVideoBean> list = new ArrayList<>();

    private boolean isSelectAll = false;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_clear_video;
    }

    @Override
    protected void initView() {
        titleTv.setText(R.string.clear_video_cache);

        clearVideoAdapter = new ClearVideoAdapter(this, list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(clearVideoAdapter);
        clearVideoAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                ClearVideoAdapter.ClearVideoBean bean = list.get(position);
                bean.setDidSelect(!bean.isDidSelect());
                //通知适配器刷新
                adapter.notifyItemChanged(position);
                //总数刷新
                refreshView();

                isSelectAll = false;
                selectIv.setSelected(false);
            }
        });
    }

    @Override
    protected void initData() {
       getVideoList();
    }

    @Override
    protected void initListener() {

    }

    private void getVideoList() {
        long totalSize = 0;
        list.clear();
        List<VideoInfoBean> videoInfoBeanList = VideoManager.getInstance().getVideoInfoBeanListExistVideo();
        for (VideoInfoBean infoBean: videoInfoBeanList) {
            ClearVideoAdapter.ClearVideoBean bean = new ClearVideoAdapter.ClearVideoBean();
            bean.setDidSelect(false);
            bean.setVideoInfoBean(infoBean);
            list.add(bean);
            totalSize += infoBean.getSize();
        }
        clearVideoAdapter.notifyDataSetChanged();

        String sizeText = getFileSizeText(totalSize);
        allSizeTv.setText(sizeText);
    }

    @OnClick(R.id.ll_select_all)
    public void onSelectAll(View v) {
        isSelectAll = !isSelectAll;
        selectIv.setSelected(isSelectAll);

        if (isSelectAll) {
            long size = 0;
            for (ClearVideoAdapter.ClearVideoBean bean : list) {
                bean.setDidSelect(true);
                size += bean.getVideoInfoBean().getSize();
            }
            if (size == 0) {
                //没有选择
                deleteTv.setText(getString(R.string.delete) + "（0KB）");
                deleteTv.setEnabled(false);
            } else {
                String text = getFileSizeText(size);
                deleteTv.setText(getString(R.string.delete) + "（" + text + "）");
                deleteTv.setEnabled(true);
            }
        } else {
            //没有选择
            for (ClearVideoAdapter.ClearVideoBean bean : list) {
                bean.setDidSelect(false);
            }
            deleteTv.setText(getString(R.string.delete) + "（0KB）");
            deleteTv.setEnabled(false);
        }
        clearVideoAdapter.notifyDataSetChanged();
    }

    private void refreshView() {
        long size = 0;
        for (ClearVideoAdapter.ClearVideoBean bean : list) {
            if (bean.isDidSelect()) {
                size += bean.getVideoInfoBean().getSize();
            }
        }
        if (size == 0) {
            //没有选择
            deleteTv.setText(getString(R.string.delete) + "（0KB）");
            deleteTv.setEnabled(false);
        } else {
            String text = getFileSizeText(size);
            deleteTv.setText(getString(R.string.delete) + "（" + text + "）");
            deleteTv.setEnabled(true);
        }
    }

    private String getFileSizeText(long filesize) {
        long kb = filesize / 1024;
        if (kb > 1024) {
            long mb = kb / 1024;
            return mb + "MB";
        } else {
            return kb + "KB";
        }
    }

    @OnClick(R.id.tv_delete)
    public void onDeleteBtnClick() {
        CommonDialog commomDialog = new CommonDialog(this, getString(R.string.delete_video_alert), null, new CommonDialog.OnCloseListener() {
            @Override
            public void onClick(Dialog dialog, boolean confirm) {
                if (confirm) {
                    deleteVideos();
                }
            }
        });
        commomDialog.show();
    }

    private void deleteVideos() {
        List<VideoInfoBean> deleteList = new ArrayList<>();
        for (ClearVideoAdapter.ClearVideoBean bean : list) {
            if (bean.isDidSelect()) {
                deleteList.add(bean.getVideoInfoBean());
            }
        }
        if (deleteList.size() == 0) {
            return;
        }
        isSelectAll = false;
        selectIv.setSelected(false);
        //删除视频
        VideoManager.getInstance().clearVideos(deleteList, new VideoManager.OnClearVideosListner() {
            @Override
            public void onClearVideos(boolean suc) {
                if (suc) {
                    getVideoList();
                    refreshView();
                }
            }
        });
    }

    @OnClick(R.id.ll_back)
    public void onBackBtnClick() {
        finish();
    }

}
